package com.example.hzq.food.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.example.hzq.food.R;
import com.example.hzq.food.adapter.NetworkImageHolderView;
import com.example.hzq.food.adapter.Shopadapter;
import com.example.hzq.food.bean.ShopBean;
import com.example.hzq.food.model.Model;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import java.util.Arrays;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hzq on 2017/6/10.
 */

public class Fragment1 extends Fragment implements  OnItemClickListener {
    private ConvenientBanner convenientBanner;//顶部广告栏控件
    private List<String> networkImages;
    private String[] image=new String[5];
    private RecyclerView recyclerView;
    private  RecyclerView.LayoutManager layoutManager;
    private Shopadapter shopadapter;
    private List<ShopBean> list=null;
    private View view=null;
    private Context context;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment1,container,false);
        context=getActivity();
        convenientBanner = (ConvenientBanner)view.findViewById(R.id.convenientBanner);
        recyclerView=(RecyclerView)view.findViewById(R.id.recyclerview);
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        shopadapter=new Shopadapter(context);
        shopadapter.setList(list);
        recyclerView.setAdapter(shopadapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        Model shopModel=new Model();
        Call<List<ShopBean>>call=shopModel.getshop();
        call.enqueue(new Callback<List<ShopBean>>() {
            @Override
            public void onResponse(Call<List<ShopBean>> call, Response<List<ShopBean>> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    list=response.body();
                    for(int i=0;i<list.size();i++){
                        image[i]=list.get(i).getPic();
                    }
                    netinit();
                    shopadapter.setList(list);
                } else {
                    Toast.makeText(context, "资源获取失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<ShopBean>> call, Throwable t) {
                Toast.makeText(context, "404", Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
    private void initImageLoader(){
        //网络图片例子,结合常用的图片缓存库UIL,你可以根据自己需求自己换其他网络图片库
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().
                showImageForEmptyUri(R.drawable.ic_default_adimage)
                .cacheInMemory(true).cacheOnDisk(true).build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                context.getApplicationContext()).defaultDisplayImageOptions(defaultOptions)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO).build();
        ImageLoader.getInstance().init(config);
    }
    private void netinit(){
        initImageLoader();
        networkImages= Arrays.asList(image);
        convenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        },networkImages).setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused})
                .setOnItemClickListener(this);;
    }

    @Override
    public void onItemClick(int position) {
        Toast.makeText(context,"点击了第"+position+"个",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResume() {
        super.onResume();
        //开始自动翻页
        convenientBanner.startTurning(3000);
    }

}
